package com.lm.forkjoin.controller;

import com.lm.forkjoin.entry.UserData;
import com.lm.forkjoin.listener.FileImportTask;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;

@RestController
public class ImportController {

    @GetMapping("/import")
    public String importData() {
        // 模拟大文件数据
        List<UserData> users = new ArrayList<>();
        for (int i = 1; i <= 100_000; i++) {
            UserData userData = new UserData();
            userData.setAge(i);
            userData.setId((long)i);
            userData.setName("User" + i);
            userData.setEmail("user" + i + "@mail.com");
            users.add(userData);
        }

        ForkJoinPool forkJoinPool = new ForkJoinPool();
        FileImportTask task = new FileImportTask(users);
        Integer result = forkJoinPool.invoke(task);

        return "导入完成，共处理数据：" + result + " 条";
    }
}

